package com.chen.blue.security.handle;

import cn.hutool.json.JSONUtil;
import com.chen.blue.constant.HttpStatus;
import com.chen.blue.result.R;
import com.chen.blue.utils.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created By CLT On 2024/4/26.
 */
@SuppressWarnings("all")
@Slf4j
@Component("accessDeniedHandler")
public class NoAccessHandler implements AccessDeniedHandler, Serializable {

    /**
     * 权限不足
     *
     * @param request
     * @param response
     * @param accessDeniedException
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.error("权限认证中");
        ServletUtil.renderString(JSONUtil.toJsonStr(R.faild(HttpStatus.C403.getCode(), HttpStatus.C403.getDesc())), response);
        return;
    }

}
